import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ebook/components/book_tile.dart';
import 'package:flutter_ebook/components/search_tile.dart';
import 'package:flutter_ebook/model/activity.dart';
import 'package:flutter_ebook/pages/home/components/book_activity.dart';
import 'package:flutter_ebook/pages/home/home_vm.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:line_icons/line_icons.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final HomeVM _viewModel = HomeVM();
  @override
  void initState() {
    super.initState();
    _viewModel.fetchActivities();
  }

  int _value = 0;

  final List<String> _activies = ["全部", "最新", "热门", "推荐"];

  final List<Map<String, dynamic>> _recommendList = [
    {
      "title": "怪画谜案",
      "image": "https://img1.doubanio.com/view/subject/s/public/s34903230.jpg",
      "author": "[日] 雨穴"
    },
    {
      "title": "食南之徒",
      "image": "https://img3.doubanio.com/view/subject/s/public/s34823157.jpg",
      "author": "马伯庸"
    },
    {
      "title": "七个证人",
      "image": "https://img9.doubanio.com/view/subject/s/public/s34896035.jpg",
      "author": "[日] 西村京太郎"
    },
    {
      "title": "早安，怪物",
      "image": "https://img3.doubanio.com/view/subject/s/public/s34798823.jpg",
      "author": "早安，怪物"
    },
    {
      "title": "三国前夜：士大夫政治与东汉皇权的崩解",
      "image": "https://img3.doubanio.com/view/subject/s/public/s34877207.jpg",
      "author": "张向荣"
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.surface,
      body: _getBodyUI(context),
    );
  }

  Widget _buildAvatar() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          "您好, 张三",
          style: TextStyle(fontSize: 24.sp, fontWeight: FontWeight.w600),
        ),
        const CircleAvatar(
          backgroundImage: AssetImage("assets/images/avatar.jpg"),
        )
      ],
    );
  }

  Widget _getBodyUI(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: _viewModel,
      builder: (context, child) {
        return SafeArea(
            child: SingleChildScrollView(
          padding: EdgeInsets.all(15.r), //内边距
          child: Column(
            children: [
              10.verticalSpace, //间距
              _buildAvatar(),
              // 搜索
              15.verticalSpace, //间距
              SearchTile(
                bookshelfTap: () {
                  print("点击书架");
                },
              ),
              15.verticalSpace, //间距
              // 读书活动 ,
              Selector<HomeVM, List<Activity>?>(
                  builder: (context, List<Activity>? activities, child) {
                    if (activities == null) {
                      return const SizedBox();
                    }
                    return BookActivity(activities: activities);
                  },
                  selector: (_, ViewModel) => ViewModel.activities),
              15.verticalSpace, //间距
              // 活动
              SizedBox(
                width: double.infinity,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    //活动标题
                    _buildActivityTitle(),
                    5.verticalSpace, //间距
                    //活动类型
                    _buildActivityType(),
                    //活动列表
                  ],
                ),
              ),
              15.verticalSpace, //间距
              //特别为你准备
              BookTile(
                books: _recommendList,
                width: 200.w,
                height: 100.h,
              )
            ],
          ),
        ));
      },
    );
  }

  Widget _buildActivityType() {
    return Wrap(
      children: List.generate(_activies.length, (index) {
        return Padding(
          padding: EdgeInsets.only(right: 10.w),
          child: ChoiceChip(
              label: Text(_activies[index]),
              selected: _value == index,
              onSelected: (value) {
                setState(() {
                  _value = index;
                });
              }),
        );
      }),
    );
  }

  Widget _buildActivityTitle() {
    return Text(
      "活动类型",
      style: TextStyle(fontSize: 24.sp, fontWeight: FontWeight.w600),
    );
  }
}
